import article from '../../api/article'
import * as types from '../mutation-type.js'

const state = {
	added: [],
	status: null
}

const getters = {
	allHistory: state => state.added
}

const actions = {
	clearHistory({commit,state}, articles) {
		const history = [...state.added]
		console.log('invoke action clearHistory')
		article.clearHistory(history, 
			() => {
				let listForClear = [...state.added]
				listForClear.forEach(item => {
					commit(types.ENABLE_ARTICLE, {id: item.id})
				})
				commit(types.CLEAR_SUCCESS);
			},
			() => commit(types.CLEAR_FAIL, {history})
		)
	}
}

const mutations = {
	[types.ADD_TO_HISTORY] (state, {id, title}) {
		state.added.push({id: id, title: title});
		console.log('mutations ADD_TO_HISTORY', state.added);
	},
	[types.CLEAR_SUCCESS] (statue) {
		console.log('mutations CLEAR_SUCCESS');
		
		state.added = []
	},
	[types.CLEAR_FAIL] (state, {history}) {
		console.log('mutations CLEAR_FAIL');
		state.added = history;
	}
}

export default {
	state,
	getters,
	actions,
	mutations
}